#include "struct.h"
#include "mytools.h"
#include <stdio.h>

void struct_declaration() {
  print_title("Struct Declaration");
  {
    // 结构体初始化及赋值
    struct car {
      int year;
      char *make; // 等价于 char make[50];
    };

    struct car car1 = {2021, "Toyota"};
    struct car car2 = {.make = "Honda", .year = 2022};
    car1.year = 2023;
    car1.make = "Ford";
    printf("car1: year=%d, make=%s\n", car1.year, car1.make);
    printf("car2: year=%d, make=%s\n", car2.year, car2.make);
    // 打印 car1 的大小
    printf("sizeof(car1)=%lu\n", sizeof(car1));
    // 打印 car2 的大小
    printf("sizeof(car2)=%lu\n", sizeof(car2));
    // 打印 car1.year 的大小
    printf("sizeof(car1.year)=%lu\n", sizeof(car1.year));
    // 打印 car1.make 的大小
    printf("sizeof(car1.make)=%lu\n", sizeof(car1.make));
  }
  {
    // 常用做法, typedef 结构体
    Person p1 = {"Alice", 25, Female};
    printf("p1: name=%s, age=%d, sex=%d\n", p1.name, p1.age, p1.sex);

    Person p2 = create_person_default();
    printf("p2: name=%s, age=%d, sex=%d\n", p2.name, p2.age, p2.sex);
  }
}

/// @brief 创建 Person 结构体的默认值
/// @details 采用函数模拟结构体初始化
/// @return Person
Person create_person_default() {
  Person p = {"Unknown", 18, Male};
  return p;
}